//
//  BaseSQLite.mm
//  ChartRefactor
//
//  Created by CaoJun on 15/4/1.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#include "BaseSQLite.h"

using namespace ytx;
using namespace std;

BaseSQLite::BaseSQLite() : sqliteDB(NULL){

}

BaseSQLite::~BaseSQLite(){
    sqlite3_close(sqliteDB);
}

const string BaseSQLite::getTableName(const Quotation * quotation){
    string id = quotation->id;
    Tools::replace(id, ".", "");
    Tools::replace(id, "+", "");
    
    return "Chart_"+ConvertToString(quotation->type)+"_"+ id +"_"+ LineType::convertLineTypeToString(quotation->quotationType);
}

bool BaseSQLite::openDB(){
    int openResult = sqlite3_open(path.c_str(), &sqliteDB);
    
    CheckResult(openResult, "open error");
    return true;
};

bool BaseSQLite::closeDB(){
    sqlite3_close(sqliteDB);
    return true;
};

bool BaseSQLite::clearTable(string tableName)
{
    if(openDB())
    {
        char* errmsg = 0;
        const string queryStr = "DROP TABLE " + tableName + "";
        int ret = sqlite3_exec(sqliteDB, queryStr.c_str(), 0, 0, &errmsg);
        CheckResult(ret, errmsg);
        sqlite3_close(sqliteDB);
        return true;
    }
    return false;
}

int BaseSQLite::handelr_exec(void *params,int n_column,char **column_value,char **column_name){
    DBResult * result = (DBResult*) params;
    result->status = true;
    
    int i;
    
    vector<string> value;
    vector<string> name;
    
    for(i=0;i<n_column;i++){
        value.push_back(ConvertToString(column_value[i]));
        name.push_back(ConvertToString(column_name[i]));
        //        printf("\t%s",column_value[i]);
    }
    //    printf("\n");
    result->row_value.push_back(value);
    result->row_name.push_back(name);
    
    //0为正确
    return SQLITE_OK;
}

string BaseSQLite::getPath()
{
    return path;
}
